//
//  WJNewFeatureViewController.swift
//  WJWeibo
//
//  Created by Jie on 15/12/18.
//  Copyright © 2015年 周文杰. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class WJNewFeatureViewController: UICollectionViewController {

    //流水布局
    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    init() {
        super.init(collectionViewLayout: layout)
    }


    //item 4
    private let itemCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareCollectionView()
    }
    
    //MARK: - 准备collectionView的相关参数
    func prepareCollectionView() {
        //注册cell
        collectionView?.registerClass(WJCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置item的大小
        layout.itemSize = view.bounds.size
        //设置不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //分页显示
        collectionView?.pagingEnabled = true
        
        //取消弹簧效果
        collectionView?.bounces = false
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! WJCollectionViewCell
        
    
        cell.index = indexPath.item + 1
        return cell
    }

 
    //当collectionView翻页停止时就调用该方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //indexpath的值不准确
        //自己计算当前的item
        let currentPath = collectionView.indexPathsForVisibleItems().last!
//        print("\(currentPath.item)")
        
        if currentPath.item == 3 {
            
            let currentCell = collectionView.cellForItemAtIndexPath(currentPath) as! WJCollectionViewCell
            if currentCell.startBtn.hidden {
                currentCell.startBtnAnim()
            }
            
        }

    }
}



//MARK: - 自定义cell
class WJCollectionViewCell : UICollectionViewCell {
    

    var index = 0 { //0-3
        //属性监测，发生值改变就会进去里面
        didSet {
            bagImg.image = UIImage(named: "new_feature_\(index)")
            startBtn.hidden = true
//            print("\(index)")
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //系统会自动调用，加载ui控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    ///加载ui
    private func prepareUI() {
        //添加控件
        contentView.addSubview(bagImg)
        contentView.addSubview(startBtn)
        
        //关闭autoresizing
        bagImg.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        //bagImg
        addConstraint(NSLayoutConstraint(item: bagImg, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: bagImg, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: bagImg, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: bagImg, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //startBtn
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        
    }
    //MARK: 按钮动画
    func startBtnAnim() {
        // 显示按钮
        startBtn.hidden = false
        
        // 设置开始按钮的scale
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //清楚transform
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                //动画完成
            
        }
    }
    
    //MARK:  懒加载
    ///背景图
    private lazy var bagImg = UIImageView(image: UIImage(named: "new_feature_1"))
    ///按钮
    private lazy var startBtn : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        return btn
    }()
    
    
}

